import { Function } from "eval5";
export default {
    inject: {
        /**
         * 注入表单实例
         */
        formVm: {
            default:()=>{}
        }
    },
    props:{
        /**
         * 控件属性
         */
        metadata: {
            type: Object
        }
    },
    computed:{
        titleStyle(){
            let style = this.metadata.style ;
            let styles = {}
            for (let key in style){
                let k = key ;
                if(key && key.includes('-')){
                    k = key.split('-');
                    k = k.map((val,idx)=>idx === 0? val : val.charAt(0).toLocaleUpperCase()+val.substring(1)).join('')
                }
                if(['textAlign','fontSize','lineHeight','fontWeight','color'].includes(k)){
                    styles[key] = style[key];
                }
            }
            return styles
        },
        /*
        * 表单控件是否只读
        */
        curReadOnly() {
            return (item,row,subTableKey) => {
                if((typeof this.formVm.readOnly ==="boolean" && this.formVm.readOnly) ||
                    (typeof this.readOnly ==="boolean" && this.readOnly)
                ){
                    return true;
                }

                //判断控件是否为只读.
                let isReadOnly = this.getConditionReadOnly(item,row);
                //判断权限是只读
                if(!isReadOnly){
                    isReadOnly=this.handReadPermission(item);
                }
                /*
               * readOnly 可以是布尔值 也可以是数组跟对象
               * */
                let {readOnly} = this ;
                if(readOnly && typeof readOnly !== "boolean"){
                    if(Array.isArray(readOnly)){
                        isReadOnly = readOnly.includes(item.field)
                    }else if(Object.prototype.toString.call(readOnly) === "[object Object]"){
                        if(subTableKey){
                            isReadOnly = readOnly[subTableKey]
                        }else {
                            isReadOnly = readOnly[item.field]
                        }
                    }
                }
                return isReadOnly ;
            }
        },
        /**
         * 获取曲线属性.
         * @returns {*}
         */
        localPermission() {
            return this.formVm.permission;
        },
        /**
         * 是否显示控件
         * 1.没有配置权限
         * 2.配置了权限.
         * @return {boolean}
         */
        showControl() {
            let layoutList = ['grid','table','subTable','oneTable','tabs','collapse','logo','title','iFrame','toolTips','grouping','dataList','button','chinese','generalField']
            return (item,row) => {
                //隐藏域
                if(!item){
                    return false
                }
                if (item.type === "hidden") {
                    return false;
                }
                //排除布局控件
                if(!layoutList.includes(item.type)){
                    //判断是否有权限.
                    let isShow = this.handShowPermission(item);
                    if(!isShow){
                        return false;
                    }
                }
                let {conditionConfig} = item.control;
                if (!conditionConfig) {
                    return true;
                }
                //没有配置则返回true
                if(!conditionConfig.condition){
                    return true;
                }
                let {condition} = conditionConfig;
                if (condition.indexOf('return') === -1) {
                    condition = 'return ' + condition;
                }
                let fn = new Function('row',condition);
                return  fn.call(this.formVm?this.formVm:this,row)

            }
        },
        /**
         * 获取控件是否必填.
         */
        curRequired() {
            return (item,row) => {
                //一对多数据直接传入判断，一对一获取 this.data判断
                row=row|| this.data;
                //如果只读则不管必填
                if(this.formVm.readOnly){
                    return false;
                }
                if(!item.field ){
                    return false;
                }
                let isSubtable = this.getIsSubTable(item);
                //使用规则校验必填
                if (item.rules) {
                    let required = false
                    item.rules.filter(res => {
                        if (res.condition) {
                            let condition = res.condition
                            if (!condition.includes('return')) {
                                condition = `return ${condition}`;
                            }
                            required = new Function("row", condition).call(this.formVm,isSubtable?row:{});
                        } else if (res.required) {
                            required = true
                        }
                    })
                    if(required){
                        return true;
                    }
                }

                //按照权限处理必填.
                return  this.handRequiredPermission(item);

            }
        }
    },
    methods:{
        handShowPermission(item){
            return this.handPermission(item,rtn=>{
              if(rtn=='none'){
                  return false;
              }
              return true;
            })
        },
        /**
         * 1.如果权限没有配置则非只读.
         * 2.读取权限如果权限为只读则返回为true.
         * @param item
         * @returns {boolean}
         */
        handReadPermission(item){
            return this.handPermission(item,(rtn)=>{
                return this.handReadResult(rtn);
            });
        },
        /**
         * 处理权限必填.
         * @param {Object} item 字段配置对象.
         * @returns {boolean|*}
         */
        handRequiredPermission(item){
            return this.handPermission(item,(rtn)=>{
                return rtn=='required'
            });
        },
        /**
         * 处理权限.
         * @param {Object} item 字段配置
         * @param {Function} callback 回调方法
         * @returns {boolean|*}
         */
        handPermission(item,callback){
            //按钮之类没field 直接返回
            if(!item.field){
                return false;
            }
            let permission=this.localPermission;
            //一对一子表
            if(item.subTable){
                let table=item.subTable;
                let rtn= permission && permission.subTable &&  permission.subTable[table]
                    &&  permission.subTable[table].fields &&  permission.subTable[table].fields[item.field];
                return callback(rtn);
            }
            //一对多子表的情况
            else if(item.controlConfig){
                let table=item.controlConfig.subTable;
                let rtn= permission && permission.subTable &&  permission.subTable[table]
                    &&  permission.subTable[table].fields &&  permission.subTable[table].fields[item.field];
                return callback(rtn);
            }
            //主表
            else{
                let rtn= permission && permission.main &&  permission.main[item.field];
                return callback(rtn);
            }

            return false;
        },
        /**
         * 判断当前是否属于子表.
         * @returns {boolean}
         */
        getIsSubTable(item){
            //子表
            if(item.controlConfig || item.subTable){
                return true;
            }
            return false;
        },
        /**
         * 1.如果有只读配置则判断只读配置。
         *   1.如果控件配置只读则返回.
         *   2.如果配置了条件则判断条件
         * 2.如果没有配置返回非只读.
         * @param item
         * @returns {boolean|*}
         */
        getConditionReadOnly(item,row){
            //一对多数据直接传入判断，一对一获取 this.data判断
            row=row|| this.data;
            let isSubTable=this.getIsSubTable(item);

            if( item.control && item.control.readOnlyConfig &&
                (item.control.readOnlyConfig.readOnly || item.control.readOnlyConfig.condition)
            ){
                let { condition,readOnly } = item.control.readOnlyConfig;
                if(readOnly){
                    return readOnly;
                }
                if(condition){
                    if(!condition.includes('return')){
                        condition = `return ${condition}`;
                    }
                    let fn = new Function('row',condition).call(this.formVm,isSubTable?row:{});
                    return fn ;
                }
            }
            return false ;
        },
        /**
         * 处理只读.
         * 1.没有设置权限则返回false
         * 2.判断权限是否为只读
         * 3.判断JSON的情况
         * @param rtn
         * @returns {boolean}
         */
        handReadResult(rtn){
            //没有设置权限则返回false
            if(rtn===undefined){
                return false;
            }
            //为r则直接返回
            if( rtn=='r'){
                return true;
            }
            //判断是否为json字符串
            if(rtn && rtn.indexOf("{")!=-1){
                let json=JSON.parse(rtn);
                return  !json.w;
            }
            return false;
        },
        /**
         * 获取双值数据的 value
         * @param {string} val 双值JSON数据
         * @returns {*|string}
         */
        getValue(val){
            if(!val){
                return "";
            }
            let json=JSON.parse(val);
            return json.value;
        },
        /**
         * 获取双值数据的 label
         * @param {string} val 双值JSON数据
         * @returns {*|string}
         */
        getLabel(val){
            if(!val){
                return "";
            }
            let json=JSON.parse(val);
            return json.label;
        },
        /**
         * 表单数据变化
         * @param formData 表单数据
         */
        dataChange(formData){
            this.$emit("dataChange", formData);
        }
    }
}
